home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / dev / c / RConfig.lha / RConfig_v1.1 / rlib / _exit.c next >
Encoding:
C/C++ Source or Header  |  1992-09-19  |  2.6 KB  |  109 lines

  1. /*
  2.  * _exit.c
  3.  * ~~~~~~~
  4.  *   Copyright (C) 1986,1987 by Manx Software Systems, Inc.
  5.  *   Copyright (C) 1992 by Anthon Pang, Omni Communications Products.
  6.  *
  7.  */
  8.  
  9. #include <fcntl.h>
  10. #include <clib/dos_protos.h>
  11. #include <clib/exec_protos.h>
  12. #include <pragmas/dos_lib.h>
  13. #include <pragmas/exec_lib.h>
  14.  
  15. /*
  16.  * globals
  17.  */
  18. extern int _argc, _arg_len;
  19. extern char **_argv, *_arg_lin;
  20. extern struct WBStartup *WBenchMsg;
  21. extern void *DOSBase, *MathBase, *MathTransBase;
  22. extern void *MathIeeeDoubBasBase, *MathIeeeDoubTransBase;
  23. extern BPTR _detach_curdir;
  24. extern void *_oldtrap, **_trapaddr;
  25.  
  26. #ifdef __RESSTART_MAIN
  27. long _H_size;
  28. #endif
  29.  
  30. void (*_cln)(void);
  31.  
  32. void _exit(int code) {
  33.     long ret = code;
  34.     register int fd;
  35.  
  36.     if (_devtab) {
  37.         for (fd = 0 ; fd < _numdev ; fd++)
  38.             close(fd);
  39.         FreeMem(_devtab, _numdev*(long)sizeof(struct _dev));
  40.     }
  41.     if (_cln)
  42.         (*_cln)();
  43.  
  44. #ifdef __DETACH_MAIN
  45.     if (_detach_curdir)             /* for DETACHed programs */
  46.         UnLock(_detach_curdir);
  47. #endif
  48.  
  49.     if (_trapaddr)                  /* clean up signal handling */
  50.         *_trapaddr = _oldtrap;
  51.     if (MathTransBase)
  52.         CloseLibrary(MathTransBase);
  53.     if (MathBase)
  54.         CloseLibrary(MathBase);
  55.     if (MathIeeeDoubBasBase)
  56.         CloseLibrary(MathIeeeDoubBasBase);
  57.     if (MathIeeeDoubTransBase)
  58.         CloseLibrary(MathIeeeDoubTransBase);
  59.     {
  60. #asm
  61.         mc68881
  62.         movem.l a5/a6,-(sp)
  63.         move.l  4,a6            ;get ExecBase
  64.         btst.b  #4,$129(a6)     ;check for 68881 flag in AttnFlags
  65.         beq     1$              ;skip if not
  66.         lea     2$,a5
  67.         jsr     -30(a6)         ;do it in supervisor mode
  68.         bra     1$
  69. 2$
  70.         clr.l   -(sp)
  71.         frestore (sp)+          ;reset the ffp stuff
  72.         rte                     ;and return
  73. 1$
  74.         move.l  (sp)+,a5/a6
  75. #endasm
  76.     }
  77.     if (WBenchMsg == 0) {
  78.         if (_arg_lin) {
  79.             FreeMem(_arg_lin, (long)_arg_len);
  80.             FreeMem(_argv, (long)(_argc+1)*sizeof(*_argv));
  81.         }
  82.     }
  83.     else {
  84.         Forbid();
  85.         ReplyMsg((struct Message *)WBenchMsg);
  86.     }
  87.     CloseLibrary(DOSBase);
  88.     {
  89. #ifdef __RESSTART_MAIN
  90. #asm
  91.         move.l  __savsp#,sp     ;get back original stack pointer
  92.         move.l  %%ret,-(sp)     ;save return exit code
  93.         lea     -32766(a4),a1
  94.         move.l  __H_size#,d0
  95.         move.l  $0004,a6
  96.         jsr     -$00d2(a6)      ;FreeMem()
  97.         move.l  (sp)+,d0
  98.         rts
  99. #endasm
  100. #else
  101. #asm
  102.         move.l  %%ret,d0        ;pick up return exit code
  103.         move.l  __savsp#,sp     ;get back original stack pointer
  104.         rts                     ;and exit
  105. #endasm
  106. #endif
  107.     }
  108. }
  109.